using System;
using UnityEngine;
using UnityEngine.UI;

public class Ranking : MonoBehaviour
{
	[Serializable]
	public class RankingData
	{
		public string playerGuid;

		public string playerName;

		public string day;

		public string flag;

		public int totalIncome;
	}

	public class RankingDataSet
	{
		public RankingData[] data;
	}

	public GameObject listView;

	public GameObject rowView;

	public GameObject loadingView;

	public CountryFlags countryFlags;

	public Color evenRowColor;

	public Color oddRowColor;

	public Color playerRowColor;

	public string type;

	public bool loadOnStart;

	private void Start()
	{
		if (loadOnStart)
		{
			Load(type);
		}
	}

	public void Load(string type)
	{
		if (loadingView != null)
		{
			loadingView.SetActive(value: true);
		}
		StartCoroutine(Api.Get("/api.php?action=GetRanking&type=" + type, DrawListView));
	}

	private void DrawListView(string json)
	{
		int num = 0;
		foreach (Transform item in listView.transform)
		{
			UnityEngine.Object.Destroy(item.gameObject);
		}
		RankingDataSet rankingDataSet = JsonUtility.FromJson<RankingDataSet>(json);
		num = 0;
		RankingData[] data = rankingDataSet.data;
		foreach (RankingData rankingData in data)
		{
			num++;
			GameObject gameObject = UnityEngine.Object.Instantiate(rowView, listView.transform);
			Text component = gameObject.transform.Find("Ranking").GetComponent<Text>();
			Image component2 = gameObject.transform.Find("Flag/Image").GetComponent<Image>();
			Text component3 = gameObject.transform.Find("Flag/Code").GetComponent<Text>();
			Text component4 = gameObject.transform.Find("Name").GetComponent<Text>();
			Text component5 = gameObject.transform.Find("Day").GetComponent<Text>();
			Text component6 = gameObject.transform.Find("Income").GetComponent<Text>();
			component.text = num.ToString();
			component2.sprite = countryFlags.GetSprite(rankingData.flag);
			component3.text = rankingData.flag;
			component4.text = rankingData.playerName;
			component5.text = rankingData.day;
			component6.text = "$" + rankingData.totalIncome.ToString("n0");
			if (num % 2 == 0)
			{
				gameObject.GetComponent<Image>().color = evenRowColor;
			}
			else
			{
				gameObject.GetComponent<Image>().color = oddRowColor;
			}
			if (rankingData.playerGuid == DO.Player.playerGuid)
			{
				gameObject.GetComponent<Image>().color = playerRowColor;
			}
			gameObject.SetActive(value: true);
		}
		if (loadingView != null)
		{
			loadingView.SetActive(value: false);
		}
	}
}
